package com.template.spring.beerportal.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PersistenceContext;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Version;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Configurable
@Entity
@Table(name = "bar_details")
public class BarDetails {

	@Id
	@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "bar"))
	@GeneratedValue(generator = "generator")
	@Column(name = "BAR_ID", unique = true, nullable = false)
	Long barId;

	@OneToOne(fetch = FetchType.LAZY)
	@PrimaryKeyJoinColumn
	Bar bar;

	@Version
	@Column(name = "version")
	private Integer version;

	@Column(name = "ADDRESS", unique = true, nullable = false, length = 256)
	private String address;

	@Column(name = "PHONE", unique = true, nullable = false, length = 32)
	private String phone;

	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}

	public Long getBarId() {
		return barId;
	}

	public void setBarId(Long barDetailsId) {
		this.barId = barDetailsId;
	}

	public Bar getBar() {
		return bar;
	}

	public void setBar(Bar bar) {
		this.bar = bar;
	}

	public Integer getVersion() {
		return this.version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return this.phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@PersistenceContext
	transient EntityManager entityManager;

	public static final EntityManager entityManager() {
		EntityManager em = new BarDetails().entityManager;
		if (em == null)
			throw new IllegalStateException(
					"Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
		return em;
	}

	// public static long countBarDetailses() {
	// return entityManager().createQuery("SELECT COUNT(o) FROM BarDetails o",
	// Long.class).getSingleResult();
	// }
	//
	// public static List<BarDetails> findAllBarDetailses() {
	// return entityManager().createQuery("SELECT o FROM BarDetails o",
	// BarDetails.class).getResultList();
	// }

	public static BarDetails findBarDetails(Long id) {
		if (id == null)
			return null;
		return entityManager().find(BarDetails.class, id);
	}

	// public static List<BarDetails> findBarDetailsEntries(int firstResult, int
	// maxResults) {
	// return entityManager().createQuery("SELECT o FROM BarDetails o",
	// BarDetails.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
	// }

	@Transactional
	public void persist() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		this.entityManager.persist(this);
	}

	// @Transactional
	// public void remove() {
	// if (this.entityManager == null) this.entityManager = entityManager();
	// if (this.entityManager.contains(this)) {
	// this.entityManager.remove(this);
	// } else {
	// BarDetails attached = BarDetails.findBarDetails(this.id);
	// this.entityManager.remove(attached);
	// }
	// }

	@Transactional
	public void flush() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		this.entityManager.flush();
	}

	@Transactional
	public void clear() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		this.entityManager.clear();
	}

	@Transactional
	public BarDetails merge() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		BarDetails merged = this.entityManager.merge(this);
		this.entityManager.flush();
		return merged;
	}

	public EntityManager getEntityManager() {
		return this.entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	public static long countBarDetailses() {
		return entityManager().createQuery("SELECT COUNT(o) FROM BarDetails o", Long.class).getSingleResult();
	}

	public static List<BarDetails> findAllBarDetailses() {
		return entityManager().createQuery("SELECT o FROM BarDetails o", BarDetails.class).getResultList();
	}

	public static List<BarDetails> findBarDetailsEntries(int firstResult, int maxResults) {
		return entityManager().createQuery("SELECT o FROM BarDetails o", BarDetails.class).setFirstResult(firstResult)
				.setMaxResults(maxResults).getResultList();
	}

	@Transactional
	public void remove() {
		if (this.entityManager == null)
			this.entityManager = entityManager();
		if (this.entityManager.contains(this)) {
			this.entityManager.remove(this);
		} else {
			BarDetails attached = BarDetails.findBarDetails(this.barId);
			this.entityManager.remove(attached);
		}
	}
}
